home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / util / misc / Fudgit233.lha / Source / src / dl / dl_loadmod.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-14  |  4.8 KB  |  190 lines

  1. /***********************************************************
  2. Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
  3. Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI not be used in advertising or publicity pertaining to
  13. distribution of the software without specific, written prior permission.
  14.  
  15. STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  16. THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  18. FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  20. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  21. OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  
  23. ******************************************************************/
  24. /*
  25. ** dl_loadmod - Load module and return address.
  26. */
  27.  
  28. #include <nlist.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31.  
  32. #include "dl.h"
  33. #ifdef DEBUG
  34. #define D(x) (x)
  35. #else
  36. #define D(x)
  37. #endif
  38.  
  39. char incr_name[512];
  40.  
  41.  
  42. int dl_loadmod_only (char *binary, char *module, char **ldnamep);
  43. extern void bzero (void *, size_t);
  44. extern int dl_remsym (char *file, char *symbol);
  45.  
  46. dl_funcptr
  47. dl_loadmod(char *binary, char *module, char *rtn)
  48. {
  49.     int rv;
  50.     struct nlist nl[2];
  51.     char *ldname;
  52.  
  53.     if ( dl_loadmod_only(binary, module, &ldname) == 0 )
  54.       return 0;
  55.     bzero((char *)nl, sizeof(nl));
  56.     nl[0].n_name = rtn;
  57.     rv = nlist(ldname, nl);
  58.     if ( rv < 0 ) {
  59.     dl_error("No valid name list in %s", ldname);
  60.     return 0;
  61.     }
  62.     if ( nl[0].n_type == 0) {
  63.     dl_error("No such symbol in binary: %s", rtn);
  64.     return 0;
  65.     }
  66.     return (dl_funcptr)nl[0].n_value;
  67. }
  68.  
  69. int
  70. dl_loadmod_mult(char *binary, char *module, struct nlist *nl)
  71. {
  72.     int i, n, rv;
  73.     char *ldname;
  74.     
  75.     if ( dl_loadmod_only(binary, module, &ldname) == 0 )
  76.       return 0;
  77.     rv = nlist(ldname, nl);
  78.     if ( rv < 0 ) {
  79.     dl_error("No valid name list in %s", ldname);
  80.     return 0;
  81.     }
  82.     n = 0;
  83.     for (i=0; nl[i].n_name; i++) {
  84.     if ( nl[0].n_type ) n++;
  85.     }
  86.     return n;
  87. }
  88.  
  89. int
  90. dl_loadmod_only(char *binary, char *module, char **ldnamep)
  91. {
  92.     int mtime, mtime2;
  93.     char *ldname;
  94.     char *libs;
  95.     long texttop, datatop;
  96.     int is_incr = 0;
  97.  
  98.     if ( binary == NULL ) {
  99.     /* No name specified. Incremental load */
  100.     if ( incr_name[0] == '\0' ) {
  101.         dl_error("Incremental load requested, but no file name", 0);
  102.         return 0;
  103.     }
  104.     binary = incr_name;
  105.     is_incr = 1;
  106.     }
  107.     /*
  108.     ** Compute expected version stamp on loadimage
  109.     */
  110.     binary = dl_getbinaryname(binary);
  111.     binary = dl_expand_script_binary(binary);
  112.     if ( (mtime=dl_gettime(binary)) <= 0) {
  113.     dl_error(0, binary); 
  114.     return 0;
  115.     }
  116.     if ( (mtime2=dl_gettime(module)) <= 0) {
  117.     dl_error(0, module);
  118.     return 0;
  119.     }
  120.     mtime = (mtime+mtime2) & 0xffff;
  121.     /*
  122.     ** Go hunt for binary.
  123.     */
  124.     D(printf("dl_loadmod: mtime=%x\n", mtime));
  125.     if ( dl_findcache(module, mtime, &ldname) == 0) {
  126.     /* Doesn't exist, or out of date. Create it. */
  127.     D(printf("dl_loadmod: not cached\n"));
  128.     libs = dl_findlibs(module);
  129.     D(printf("dl_loadmod: libs=%s\n", libs));
  130.     if ( !dl_hashaddrs(module, 1, 1, &texttop, &datatop) ) {
  131.         dl_error("dl_loadmod: no memory space for module", 0);
  132.         return 0;
  133.     }
  134.     if ( !dl_linkfile(ldname, binary, module, libs, texttop,
  135.               datatop, mtime) )
  136.       return 0;
  137.     /* XXXX Should check here that the newly-linked module actually fits */
  138.     }
  139.     if ( is_incr ) {
  140.     D(printf("dl_loadmod: zapping _end, _etext and _edata\n"));
  141.     dl_remsym(ldname, "_end");
  142.     dl_remsym(ldname, "_etext");
  143.     dl_remsym(ldname, "_edata");
  144.     }
  145.     D(printf("dl_loadmod: cache ok, loading %s\n", ldname));
  146.     if( dl_ldfile(ldname) == 0)
  147.       return 0;
  148.     *ldnamep = ldname;
  149.     strncpy(incr_name, ldname, 511);
  150.     return 1;
  151. }
  152.  
  153. void
  154. dl_setincr(char *name)
  155. {
  156.     name = dl_expand_script_binary(name);
  157.     strncpy(incr_name, name, 511);
  158. }
  159.  
  160. char *
  161. dl_expand_script_binary(char *name)
  162. {
  163.     FILE *fp;
  164.     static char buf[512];
  165.     char *bp;
  166.     char *endp;
  167.  
  168.     if ( name == 0 )
  169.       return name;
  170.     if ( (fp=fopen(name, "r")) == NULL ) {
  171.     /* Cannot open file, so just use given name */
  172.     return name;
  173.     }
  174.     if ( fgets(buf, 511, fp) == NULL ) {
  175.     fclose(fp);
  176.     return name;
  177.     }
  178.     fclose(fp);
  179.     if ( buf[0] != '#' || buf[1] != '!' )
  180.       return name;
  181.     buf[strlen(buf)-1] = '\0';
  182.     endp = strchr(buf, ' ');
  183.     if ( endp ) *endp = '\0';
  184.     bp = buf + 2;
  185.     while ( *bp == ' ' )
  186.       bp++;
  187.     D(printf("dl_expand_script_binary: binary in %s\n", bp));
  188.     return bp;
  189. }
  190.